package com.ruoyi.hr.controller.hr.welfare.tsgjj;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.tsgjj.Tsgjj;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.welfare.tsgjj.TsgjjService;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 特殊人员登记
 * @date 2020/1/3 10:48
 */
@Controller
@RequestMapping("/welfare/tsgjj")
public class TsgjjController extends BaseController {

    @Autowired
    private TsgjjService tsgjjService;

    @Autowired
    AuthorityService authorityService;


    private String prefix = "hr/welfare/tsgjj";

    /**
     * 福利类型跳转
     * @return
     */
    @GetMapping()
    public String tsgjj() {
        return prefix + "/tsgjj";
    }

    /**
     * 分页数据
     *
     * @param tsgjj
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Tsgjj tsgjj) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        List<Tsgjj> list = tsgjjService.findTsgjj(tsgjj,authorityList,isAdmin);
        return getDataTable(list);
    }

    /**
     * 新增跳转
     *
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap) {
        mmap.put("tsgjj", tsgjjService.findTsgjjById(id));
        return prefix + "/edit";
    }

    /**
     * 输入工号获取员工信息
     */
    @RequestMapping("/findEmpInfo")
    @ResponseBody
    public Map<String, Object> findEmpInfo(String badge) {
        Map<String,Object> map = tsgjjService.findEmpInfo(badge);
        if(null == map){
            map = new HashMap<String,Object>();
            map.put("msg","error");
        }else{
            map.put("msg","success");
        }
        return map;
    }

    @PostMapping("/add/tsgjj")
    @ResponseBody
    public AjaxResult addTsgjj(Tsgjj tsgjj){
        return toAjax(tsgjjService.insertTsgjj(tsgjj));
    }

    @PostMapping("/edit/tsgjj")
    @ResponseBody
    public AjaxResult editTsgjj(Tsgjj tsgjj){
        return toAjax(tsgjjService.updateTsgjj(tsgjj));
    }


    /**
     * 删除福利基数
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tsgjjService.deleteTsgjjByIds(ids));
    }
}

